package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author by mycp
 * @Classname TravelController
 * @Description TODO
 * @Date 2018/11/5 12:49
 */
@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    /**
     * 查询待审核状态 / 拒绝 / 两者状态的游记
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        // 如果没有设置查询状态, 则默认查询
        if (qo.getState() == null){
            qo.setState(Travel.STATE_WAIT);
        }
        // 只有用户的游记愿意公布才进行审核
        qo.setIsPublic(true);
        // 根据用户的更新时间升序排序--防止较早更新的游记没有进行审核
        qo.setOrderBy("t.lastUpdateTime asc");

        PageInfo pageInfo = travelService.queryTravel(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "travel/list";
    }


    /**
     * 根据游记的id获取该游记的富文本内容
     * @param travelId
     * @return
     */
    @RequestMapping("/getContent")
    @ResponseBody
    public Object getTravelContent(Long id){
        TravelContent travelContent = travelService.getTravelContentById(id);
        return travelContent;
    }

    /**
     * 更改游记状态
     * @param id
     * @param state
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeTravelState(Long id, Integer state){
        JsonResult result = new JsonResult();
        travelService.changeTravelState(id, state);
        return result;
    }
}
